//
//  CGLodingView.m
//  Onskysell
//
//  Created by cg on 17/5/25.
//  Copyright © 2017年 cg. All rights reserved.
//

#import "CGLodingView.h"

@implementation CGLodingView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupView];
        
        [self addConstraint];
    }
    return self;
}

+ (CGLodingView *)shareInstance{
    static CGLodingView *loding = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loding = [[CGLodingView alloc]init];
    });
    return loding;
}


- (void)setupView{
    self.isFirstLoad = YES;
    
    [self setBackgroundColor:[UIColor clearColor]];
    [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    self.bgView = [[UIView alloc] init];
    [self.bgView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.6]];
    [self addSubview:self.bgView];
    
    UIView *statusBar = [LJUtility getStatusBar];
    self.statusBarBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, statusBar?CGRectGetHeight(statusBar.bounds):20)];
    [self.statusBarBgView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.6]];
    
    self.indicatro = [[UIActivityIndicatorView alloc] init];
    self.indicatro.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.indicatro.color = [UIColor darkGrayColor];
    [self addSubview:self.indicatro];
    
    self.lodingLabel = [[UILabel alloc]init];
    [self.lodingLabel setFont:[UIFont systemFontOfSize:14]];
    [self.lodingLabel setTextColor:[UIColor darkGrayColor]];
    [self.lodingLabel setTextAlignment:NSTextAlignmentCenter];
    [self.lodingLabel setText:@"加载中."];
    [self addSubview:self.lodingLabel];
    
    [self.loadingArray addObject:@"加载中."];
    [self.loadingArray addObject:@"加载中.."];
    [self.loadingArray addObject:@"加载中..."];
    
}

- (void)addConstraint{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.indicatro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(20));
        make.height.equalTo(@(20));
    }];
    
    [self.lodingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.indicatro.mas_bottom);
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(30));
    }];
}

- (void)startAnimation{
    for (UIView *v in KWindow.subviews) {
        if ([v isMemberOfClass:self.class]) {
            [v removeFromSuperview];
            [self.statusBarBgView removeFromSuperview];
        }
    }
    
    [KWindow addSubview:self];
    UIView *statusBar = [LJUtility getStatusBar];
    if (!self.isFirstLoad) {
        if (statusBar) {
            [statusBar addSubview:self.statusBarBgView];
        }
    }
    else self.isFirstLoad = NO;
    
    [self.indicatro startAnimating];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(action:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    self.time = 0;
    [self.lodingLabel setText:@"加载中."];
}

- (void)stopAnimation{
    [self.indicatro stopAnimating];
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    [self.statusBarBgView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)dealloc{
    [self.indicatro stopAnimating];
    [self.timer invalidate];
    self.timer = nil;
}

- (void)action:(NSTimer *)timer{
    [self.lodingLabel setText:[self.loadingArray objectAtIndex:self.time]];
    self.time ++;
    if (self.time == 3) {
        self.time = 0;
    }
}

- (NSMutableArray *)loadingArray{
    if (!_loadingArray) {
        _loadingArray = [[NSMutableArray alloc]init];
    }
    return _loadingArray;
}

@end
